package com.ericsson.rm.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ericsson.rm.dao.ProjectResourceDao;
import com.ericsson.rm.dao.ResourceAllocateDao;
import com.ericsson.rm.model.EmpDetail;
import com.ericsson.rm.model.ProjectDetail;
import com.ericsson.rm.model.ProjectResourceDetail;
import com.ericsson.rm.response.EmployeeStatusInDBResponse;
import com.ericsson.rm.response.ProjectDetailsResponse;
import com.ericsson.rm.response.ProjectResourceDetailResponse;
import com.ericsson.rm.service.ResourceAllocateService;
import com.ericsson.rm.vo.ProjectResourceDetailVO;

@Service
public class ResourceAllocateServiceImpl implements ResourceAllocateService {

	@Autowired
	private ResourceAllocateDao resourceAllocateDao;
	
	@Autowired
	private ProjectResourceDao projectResourceDao;
	
	@Override
	public EmployeeStatusInDBResponse checkEmpIDfromTable(String empID)
	{
		System.out.println("in checkEmpIDfromTable() service impl...........................");
		String status = null;
		EmployeeStatusInDBResponse empStatus = new EmployeeStatusInDBResponse();
		EmpDetail empDetail = resourceAllocateDao.checkEmployeeIfPresentInDB(empID);
		if(empDetail != null){
			status = "Entity is found";
		} else {
			status = "Entity not found with Id : "+empID;
		}
		empStatus.setStatus(status);
		return empStatus;
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public List<String> getProjNameFromTable(String projName)
	{
		System.out.println("in getProjNameFromTable() service impl....................................... ");
		
		//ProjectDetailsResponse projectNameResponse = new ProjectDetailsResponse();
		List<String> projNamelist = null;
		List<String> listofProjs = resourceAllocateDao.getProjectNameFromDB(projName);
		//List<ProjectDetail> projdetails = new ArrayList<ProjectDetail>();
			System.out.println(listofProjs);		
		if(!CollectionUtils.isEmpty(listofProjs))		
			projNamelist = new ArrayList<String>();
		
		for(String p : listofProjs){
			projNamelist.add(p);
		}
		
		/*projectNameResponse.setProjectNames(projNamelist);
		System.out.println("service imple is returning "+projectNameResponse.getProjectNames());
		return projectNameResponse;*/
		return projNamelist;
	}
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public List<ProjectDetailsResponse> getProjectDetailFromTable(String projName)
	{
		List<ProjectDetailsResponse> projdetail = resourceAllocateDao.getProjectDetailFromDB(projName);
		return projdetail;
	}
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public List<ProjectResourceDetailResponse> getProjectResourceDetailFromTable(String projId)
	{
		List<ProjectResourceDetail> details = projectResourceDao.fetchProjectResourceDetailFromDB(projId);
		System.out.println("details "+details);
		List<ProjectResourceDetailResponse> projresourcelist = new ArrayList<ProjectResourceDetailResponse>();
		if(details != null && !details.isEmpty()){
		for (Iterator<ProjectResourceDetail> iterator = details.iterator(); 
				iterator.hasNext();) {
			ProjectResourceDetail detail =  iterator.next();
			
			/*ProjectResourceDetailVO detailVO = new ProjectResourceDetailVO(detail.getResourceDetail().getResourceId()"1234567890", 
					detail.getProjectDetail().getProjectId()"987654321",
					detail.getProjectResourceId(),detail.getAllocationStartDate(), 
					detail.getAllocationEndDate(), detail.getAllocationPercentage());*/
			
			ProjectResourceDetailResponse detailresp = new ProjectResourceDetailResponse();
			detailresp.setAllocation_end_date(detail.getAllocationEndDate());
			detailresp.setAllocation_percentage(detail.getAllocationPercentage());
			detailresp.setAllocation_start_date(detail.getAllocationStartDate());
			detailresp.setProj_resource_id(detail.getProjectResourceId());
			detailresp.setProjId(detail.getProjectDetail().getProjectId());
			detailresp.setResourceID(detail.getResourceDetail().getResourceId());
			detailresp.setSignum(detail.getResourceDetail().getSignum());
			
			projresourcelist.add(detailresp);
		  }
		}
		return projresourcelist;
	}

	
	//getters and setters...................
	public ResourceAllocateDao getResourceAllocateDao() {
		return resourceAllocateDao;
	}

	public void setResourceAllotcateDao(ResourceAllocateDao resourceAllocateDao) {
		this.resourceAllocateDao = resourceAllocateDao;
	}

	public ProjectResourceDao getProjectResourceDao() {
		return projectResourceDao;
	}

	public void setProjectResourceDao(ProjectResourceDao projectResourceDao) {
		this.projectResourceDao = projectResourceDao;
	}
	
	
}
